#!/bin/gawk -f
#
# Convert Aqua restraint file to XPLOR format
#

BEGIN {
    if ( !force ) force = 1.0
    if ( !expon ) expon = 2
}

NR < 3 {
    print "!", $0
    next
}

/^#/ {
    sub( /^#/, "!" )
    print
    next
}

/ANGLE/ {
    up = $5
    lo = $6
    angle = ( up + lo ) / 2
    delta = ( up - lo ) / 2
}

/ANGLE.*PHI/ {
    printf( "assign (resid %3i and name c )   (resid %3i and name n )\n", 
	$3 - 1, $3 )
    printf( "       (resid %3i and name ca)   (resid %3i and name c )  %.1f %6.1f %6.1f %i\n", 
        $3, $3, force, angle, delta, expon )
    next
}

/ANGLE.*PSI/ {
    printf( "assign (resid %3i and name n )   (resid %3i and name ca)\n", 
	$3 , $3 )
    printf( "       (resid %3i and name c )   (resid %3i and name n )  %f %6.1f %6.1f %i\n", 
        $3, $3 + 1, force, angle, delta, expon )
    next
}

/ANGLE.*OMEGA/ {
    printf( "assign (resid %3i and name ca)   (resid %3i and name c )\n", 
	$3 - 1, $3 - 1)
    printf( "       (resid %3i and name n )   (resid %3i and name ca)  %f %6.1f %6.1f %i\n", 
        $3, $3, force, angle, delta, expon )
    next
}

/ANGLE.*CHI1/ {
    printf( "assign (resid %3i and name n )   (resid %3i and name ca)\n", 
	$3 , $3 )
    printf( "       (resid %3i and name cb)   (resid %3i and name xx)  %f %6.1f %6.1f %i\n", 
        $3, $3 , force, angle, delta, expon )
    next
}

$1 ~ /UPLO$/ {
    ii = $9
    jj = 0
    kk = $8 - ii
    printf( "assign (resid %3i and name %s)   (resid %3i and name %s) %.2f %.2f %.2f\n",
	$3, $4, $6, $7, ii, jj, kk )
    next
}

    {
    print "NOT CONVERTED:", $0
    next
}
